﻿using King.Box.DbProvider;
using King.Box.Utils;
using PsCat.Aspet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace King.Box.Service
{
    /// <summary>
    /// 基础增删改查
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DataService<T> :IDataService<T> where T : class, new()
    {
        public DataService() { }

        public DataService(string constr = "")
        {
            SingletonBase<DataProvider<T>>.Initialize(new DataProvider<T>(constr));
        }
        
        public T Get(object col)
        {
            return SingletonBase<DataProvider<T>>.Instance.Get(col);
        }

        public int Count(Expression<Func<T, bool>> filter)
        {
            return SingletonBase<DataProvider<T>>.Instance.Count(filter);
        }

        [CoreCat]
        public T Get(Expression<Func<T, bool>> filter, string includeProperties = "")
        {
            return SingletonBase<DataProvider<T>>.Instance.Get(filter, includeProperties);
        }

        public IEnumerable<T> Select(Expression<Func<T, dynamic>> dynamicPredicate, Func<dynamic, T> dynamicFunc, Expression<Func<T, bool>> fiter = null,
            Func<IQueryable<T>, IOrderedQueryable<T>> orderby = null, string includeProperties = "")
        {
            return SingletonBase<DataProvider<T>>.Instance.Select(dynamicPredicate, dynamicFunc, fiter, orderby, includeProperties);
        }
        
        public IEnumerable<T> Select(Expression<Func<T, bool>> fiter = null,
               Func<IQueryable<T>, IOrderedQueryable<T>> orderby = null, string includeProperties = "", int count = 0)
        {
            return SingletonBase<DataProvider<T>>.Instance.Select(fiter, orderby, includeProperties, count);
        }
        
        public IEnumerable<T> Select(out int totalcount, Expression<Func<T, bool>> fiter = null, int pi = 1, int pz = 0, Func<IQueryable<T>, IOrderedQueryable<T>> orderby = null, string includeProperties = "")
        {
            return SingletonBase<DataProvider<T>>.Instance.Select(out totalcount, fiter, pi, pz, orderby, includeProperties);
        }
        
        public bool AddEntity(T t_entity)
        {
            return SingletonBase<DataProvider<T>>.Instance.Insert(t_entity);
        }
        
        public bool Update(T t_enttiy)
        {
            return SingletonBase<DataProvider<T>>.Instance.Update(t_enttiy);
        }

        public IEnumerable<object> SelectBySql(string query, bool otherdb = true)
        {
            return SingletonBase<DataProvider<T>>.Instance.SelectBySql<T>(query, otherdb);
        }
        
        public IEnumerable<T> SelectBySql(out int totalCount, string query, int pageIndex, int pageSize)
        {
            return SingletonBase<DataProvider<T>>.Instance.SelectBySql<T>(out totalCount, query, pageIndex, pageSize);
        }
        
        public IEnumerable<T> DeleteAll(Expression<Func<T, bool>> predicate)
        {
            return SingletonBase<DataProvider<T>>.Instance.DeleteAll(predicate);
        }
        
        public IEnumerable<T> UpdateAll(List<T> list)
        {
            return SingletonBase<DataProvider<T>>.Instance.UpdateAll(list);
        }
        
        public IEnumerable<T> InsertAll(List<T> list)
        {
            return SingletonBase<DataProvider<T>>.Instance.InsertAll(list);
        }

        public bool Update(T model, string[] arr)
        {
            return SingletonBase<DataProvider<T>>.Instance.Update(model, arr);
        }
    }
}
